package rd.hardcore.engine.sample.source {
	import flash.utils.ByteArray;
	import flash.media.SoundLoaderContext;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.media.Sound;

	/**
	 * @author plepers
	 */
	public class MP3Source extends AbstractSource {
		
		public function MP3Source (file : File) {
			super( file );
		}

		
		override public function extract () : void {
			super.extract( );
			
			_file = File.desktopDirectory.resolvePath( _file.name  );
			_file.canonicalize();
			trace( _file.isSymbolicLink );
			trace( _file.nativePath );
			var req : URLRequest =  new URLRequest( "file://"+_file.nativePath );
			trace( req.url );
			
			var s : Sound = new Sound( req );
			s.addEventListener( Event.COMPLETE, _onSoundLoaded );
			
			trace( "ca roule" );
		}

		private function _onSoundLoaded (event : Event) : void {
			trace( "c meme loadé didon" );
			var s : Sound = event.target as Sound;
			trace( s.url );
			s.removeEventListener( Event.COMPLETE, _onSoundLoaded );
			var raw : ByteArray = new ByteArray();
//			var available : Number = s.extract( raw, 32 );
//			trace( "mp3 extract len : " + available + 32 );
//			trace( "reste : " ,s.extract( raw, available ) );
//			trace( "mp3 extract len2 : " + raw.length );
			var available : Number;
			var c : int = 0;
			do {
				available = s.extract( raw, 512 );
				c++;
			} while (available > 0);
			
			trace( "sampled ", c, c * 512, c*256/44100 );
			
			_fillRawDatas( raw );
			
			s.close();
			
		}
	}
}
